#!/bin/bash

# 远程服务器容器部署脚本
# Redis 和 MySQL 容器创建

set -e

echo "=========================================="
echo "  远程服务器容器部署"
echo "=========================================="
echo ""

# 配置
MYSQL_PASSWORD="20shagua"
MYSQL_PORT=13306
REDIS_PASSWORD="20shagua"
REDIS_PORT=16379

# 1. 停止并删除旧容器（如果存在）
echo "[1/5] 清理旧容器..."
docker stop tzai-mysql tzai-redis 2>/dev/null || true
docker rm tzai-mysql tzai-redis 2>/dev/null || true
echo "✓ 旧容器已清理"

# 2. 创建数据目录
echo ""
echo "[2/5] 创建数据目录..."
mkdir -p /data/mysql
mkdir -p /data/redis
echo "✓ 数据目录已创建"

# 3. 创建 MySQL 容器
echo ""
echo "[3/5] 创建 MySQL 容器..."
docker run -d \
  --name tzai-mysql \
  --restart always \
  -p ${MYSQL_PORT}:3306 \
  -e MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD} \
  -e MYSQL_DATABASE=tzai \
  -e MYSQL_USER=tzai \
  -e MYSQL_PASSWORD=${MYSQL_PASSWORD} \
  -e TZ=Asia/Shanghai \
  -v /data/mysql:/var/lib/mysql \
  mysql:8.0 \
  --character-set-server=utf8mb4 \
  --collation-server=utf8mb4_unicode_ci \
  --default-authentication-plugin=mysql_native_password

if [ $? -eq 0 ]; then
    echo "✓ MySQL 容器创建成功"
else
    echo "✗ MySQL 容器创建失败"
    exit 1
fi

# 4. 创建 Redis 容器
echo ""
echo "[4/5] 创建 Redis 容器..."
docker run -d \
  --name tzai-redis \
  --restart always \
  -p ${REDIS_PORT}:6379 \
  -v /data/redis:/data \
  redis:6.2 \
  redis-server --requirepass ${REDIS_PASSWORD} --appendonly yes

if [ $? -eq 0 ]; then
    echo "✓ Redis 容器创建成功"
else
    echo "✗ Redis 容器创建失败"
    exit 1
fi

# 5. 等待容器启动
echo ""
echo "[5/5] 等待容器启动..."
sleep 10

# 检查容器状态
echo ""
echo "=========================================="
echo "  容器状态"
echo "=========================================="
docker ps --filter "name=tzai-" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

echo ""
echo "=========================================="
echo "  连接信息"
echo "=========================================="
echo ""
echo "MySQL 连接信息："
echo "  主机: 103.47.80.172"
echo "  端口: ${MYSQL_PORT}"
echo "  用户: root"
echo "  密码: ${MYSQL_PASSWORD}"
echo "  数据库: tzai"
echo ""
echo "Redis 连接信息："
echo "  主机: 103.47.80.172"
echo "  端口: ${REDIS_PORT}"
echo "  密码: ${REDIS_PASSWORD}"
echo ""

# 测试连接
echo "=========================================="
echo "  测试连接"
echo "=========================================="
echo ""

# 测试 MySQL
echo "测试 MySQL 连接..."
docker exec tzai-mysql mysql -uroot -p${MYSQL_PASSWORD} -e "SELECT VERSION();" 2>/dev/null
if [ $? -eq 0 ]; then
    echo "✓ MySQL 连接成功"
else
    echo "✗ MySQL 连接失败"
fi

echo ""

# 测试 Redis
echo "测试 Redis 连接..."
docker exec tzai-redis redis-cli -a ${REDIS_PASSWORD} ping 2>/dev/null
if [ $? -eq 0 ]; then
    echo "✓ Redis 连接成功"
else
    echo "✗ Redis 连接失败"
fi

echo ""
echo "=========================================="
echo "  部署完成"
echo "=========================================="
echo ""
echo "常用管理命令："
echo "  查看容器: docker ps"
echo "  查看日志: docker logs <容器名>"
echo "  重启容器: docker restart <容器名>"
echo "  停止容器: docker stop <容器名>"
echo "  进入容器: docker exec -it <容器名> bash"
echo ""
echo "MySQL 命令："
echo "  docker exec -it tzai-mysql mysql -uroot -p${MYSQL_PASSWORD}"
echo ""
echo "Redis 命令："
echo "  docker exec -it tzai-redis redis-cli -a ${REDIS_PASSWORD}"
echo ""

echo "🎉 部署成功！"

